<template>
  <div class="app-container">
    <div style="width:100%; text-align: right;">
      <el-button size="small" type="primary" icon="el-icon-download" @click="exportData">导出</el-button>
    </div>

    <div style="width: 100%; margin-top: 8px;">
      <el-form :model="listQuery" label-width="80px">
        <div style="display: flex;">
          <el-form-item label="开始时间">
            <el-date-picker
                v-model="listQuery.beginTime"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择开始时间"
                align="right"
                :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间" style="margin-left: 10px;">
            <el-date-picker
                style="display: inline-block;"
                v-model="listQuery.endTime"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择结束时间"
                align="right"
                :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="业务类型" style="margin-left: 10px;">
            <el-select
                v-model="listQuery.businessType"
                size="small"
                placeholder="选择业务类型"
                clearable
                class="filter-item">
              <el-option v-for="(item,index) in businessTypeOptions" :label="item.label" :value="item.value" :key="index" />
            </el-select>
          </el-form-item>
        </div>
        <div style="display: flex;">
          <el-form-item label="操作状态" style="margin-left: 10px;">
            <el-select
              v-model="listQuery.status"
              size="small"
              placeholder="选择操作状态"
              clearable
              class="filter-item">
              <el-option v-for="(item,index) in statusOptions" :label="item.label" :value="item.value" :key="index" />
            </el-select>
          </el-form-item>
          <el-form-item label="操作模块" style="margin-left: 10px;">
            <el-input
              v-model="listQuery.title"
              size="small"
              placeholder="请输入模块名称"
              clearable
              class="filter-item w-200"/>
          </el-form-item>
          <el-form-item label="用户名" style="margin-left: 10px;">
            <el-input
                v-model="listQuery.operName"
                size="small"
                placeholder="请输入用户名"
                clearable
                class="filter-item w-200"/>
          </el-form-item>
          <el-button-group class="filter-item" style="margin-left: 16px;">
            <el-button
              size="small"
              type="primary"
              icon="el-icon-search"
              @click="search">
              搜索
            </el-button>
            <el-button
              size="small"
              type="primary"
              icon="el-icon-refresh"
              @click="refresh">
              重置
            </el-button>
          </el-button-group>
        </div>
      </el-form>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading" stripe
      highlight-current-row>
      <el-table-column label="ID" width="70" prop="operId" />
      <el-table-column label="操作模块" width="180" prop="title" />
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          <template v-if="scope.row.status === 0">
            <el-tag type="success">正常</el-tag>
          </template>
          <template v-else>
            <el-tag type="danger">异常</el-tag>
          </template>
        </template>
      </el-table-column>
      <el-table-column
          label="操作时间"
          width="160" prop="operTime" />
      <el-table-column label="操作人" width="100" prop="operName" />
      <el-table-column
          label="业务类型"
          width="120">
        <template slot-scope="scope">
          <template v-if="scope.row.businessType === 1">新增</template>
          <template v-else-if="scope.row.businessType === 2">修改</template>
          <template v-else-if="scope.row.businessType === 3">删除</template>
          <template v-else-if="scope.row.businessType === 4">授权</template>
          <template v-else-if="scope.row.businessType === 5">导出</template>
          <template v-else-if="scope.row.businessType === 6">导入</template>
          <template v-else-if="scope.row.businessType === 7">强退</template>
          <template v-else-if="scope.row.businessType === 8">生成代码</template>
          <template v-else-if="scope.row.businessType === 9">清空数据</template>
          <template v-else>其它</template>
        </template>
      </el-table-column>
      <el-table-column label="IP地址" prop="operIp" />
      <el-table-column label="接口地址" prop="operUrl" />
      <el-table-column fixed="right" label="操作" width="80" align="center">
        <template slot-scope="scope">
          <span>
            <el-button plain circle type="success" icon="el-icon-more" size="mini" @click="detail(scope.row)" title="详情"></el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="fetchData"
    />

    <el-dialog :visible.sync="dialogVisible" title="详情">
      <el-form ref="dataForm" :model="detailData" label-width="120px" label-position="right" v-if="detailData">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="Id">
              <span>{{detailData.operId}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发生时间">
              <span>{{detailData.operTime}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作模块">
              <span>{{detailData.title}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户名">
              <span>{{detailData.operName}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="IP地址">
              <span>{{detailData.operIp}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业务类型">
              <span>
                <template v-if="detailData.businessType === 1">新增</template>
                <template v-else-if="detailData.businessType === 2">修改</template>
                <template v-else-if="detailData.businessType === 3">删除</template>
                <template v-else-if="detailData.businessType === 4">授权</template>
                <template v-else-if="detailData.businessType === 5">导出</template>
                <template v-else-if="detailData.businessType === 6">导入</template>
                <template v-else-if="detailData.businessType === 7">强退</template>
                <template v-else-if="detailData.businessType === 8">生成代码</template>
                <template v-else-if="detailData.businessType === 9">清空数据</template>
                <template v-else>其它</template>
              </span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求方法">
              <span>{{detailData.requestMethod}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <span> {{ detailData.status === 0 ? '正常' : '异常' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="错误消息">
              <span> {{ detailData.errorMsg }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="接口地址">
              <span>{{detailData.operUrl}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="方法">
              <span>{{detailData.method}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="操作结果">
              <span>{{detailData.jsonResult}}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { getList, exportData } from '@/api/log'

export default {
  components: {
    Pagination
  },
  data() {
    return {
      detailData: undefined,
      dialogVisible: false,
      total: 0,
      list: [],
      listLoading: true,
      statusOptions: [
        {label: '正常', value: 0},
        {label: '异常', value: 1},
      ],
      businessTypeOptions: [
        {label: '其它', value: 0},
        {label: '新增', value: 1},
        {label: '修改', value: 2},
        {label: '删除', value: 3},
        {label: '授权', value: 4},
        {label: '导出', value: 5},
        {label: '导入', value: 6},
        {label: '强退', value: 7},
        {label: '生成代码', value: 8},
        {label: '清空数据', value: 9}
      ],
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        beginTime: undefined,
        endTime: undefined,
        title: undefined,
        status: undefined,
        operName: undefined,
        businessType: undefined
      },
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    exportData() {
      let queryParams = {
        beginTime: this.listQuery.beginTime,
        endTime: this.listQuery.endTime,
        title: this.listQuery.title,
        status: this.listQuery.status,
        operName: this.listQuery.operName,
        businessType: this.listQuery.businessType
      }
      exportData(queryParams).then(response => {
        if(response.code === 200) {
          let filename = response.msg
          let url = '/download/' + filename
          let link = document.createElement("a");
          link.style.display = "none";
          link.href = url;
          link.setAttribute("download", filename); //指定下载后的文件名，防跳转
          document.body.appendChild(link);
          link.click();
        }
      })
    },
    search() {
      this.fetchData()
    },
    refresh() {
      this.listQuery = {
        pageNum: 1,
        pageSize: 10,
        beginTime: undefined,
        endTime: undefined,
        title: undefined,
        status: undefined,
        operName: undefined,
        businessType: undefined
      }
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        if(response.code === 200) {
          this.list = response.rows
          this.total = response.total
          this.listLoading = false
        } else {
          this.$message.warning("获取列表失败：" + response.msg);
          this.listLoading = false
        }
      })
    },
    detail(row) {
      this.detailData = row
      this.dialogVisible = true
    }
  }
}
</script>
